// DlgMain.cpp : implementation file
//

#include "stdafx.h"
#include "DlgMain.h"
#include <io.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgMain dialog


CDlgMain::CDlgMain(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgMain::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgMain)
	m_csFilePath = _T("date\\question1.txt");
	m_csdata = _T("");
	//}}AFX_DATA_INIT
}


void CDlgMain::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgMain)
	DDX_Text(pDX, IDC_EDIT_FILEPATH, m_csFilePath);
	DDX_Text(pDX, IDC_EDIT_INFO, m_csdata);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgMain, CDialog)
	//{{AFX_MSG_MAP(CDlgMain)
	ON_BN_CLICKED(IDC_BN_OPEN, OnBnOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgMain message handlers

BOOL CDlgMain::OnInitDialog() 
{
	CDialog::OnInitDialog();
	::CoInitialize(NULL);
	// TODO: Add extra initialization here
	///::MessageBox(NULL,"","",MB_OK);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
typedef HANDLE(
WINAPI *
IALibCreateFileA)(
			LPCSTR lpFileName,
			DWORD dwDesiredAccess,
			DWORD dwShareMode,
			LPSECURITY_ATTRIBUTES lpSecurityAttributes,
			DWORD dwCreationDisposition,
			DWORD dwFlagsAndAttributes,
			HANDLE hTemplateFile
			);
typedef BOOL (
			WINAPI *
			IALibReadFile)(
			HANDLE hFile,
			LPVOID lpBuffer,
			DWORD nNumberOfBytesToRead,
			LPDWORD lpNumberOfBytesRead,
			LPOVERLAPPED lpOverlapped
			);
typedef 
BOOL (
WINAPI *
IALibCloseHandle)(
			HANDLE hObject
			);
IALibCreateFileA	g_CreateF = (IALibCreateFileA)GetProcAddress(::LoadLibraryA("IALib.DLL"),"M1");
IALibReadFile		g_ReadF = (IALibReadFile)GetProcAddress(::LoadLibraryA("IALib.DLL"),"M6");
IALibCloseHandle	g_CloseH = (IALibCloseHandle)GetProcAddress(::LoadLibraryA("IALib.DLL"),"M23");
int DatatoFile(char *srcfilename,char * srcdestname)
{	
	HANDLE hFile; 
	DWORD dwBytesRead =1024;
	char szbuff[1024] = {0};
	FILE * pfile = fopen(srcdestname,"wb+");
	hFile = g_CreateF(srcfilename,               // file to open
		GENERIC_READ,          // open for reading
		FILE_SHARE_READ,       // share for reading
		NULL,                  // default security
		OPEN_EXISTING,         // existing file only
		FILE_ATTRIBUTE_NORMAL, // normal file
		NULL);                 // no attr. template
	
    if (hFile == INVALID_HANDLE_VALUE) 
    { 
        printf("Could not open file (error %d)\n", GetLastError());
		if (pfile)
		{
			fclose(pfile);
		}
        return 0; 
    }
	g_ReadF(hFile, szbuff, 1024, &dwBytesRead, NULL);
    for(;dwBytesRead;)
    {
		if (pfile)
		{
			fwrite(szbuff,dwBytesRead,1,pfile);
		}
		g_ReadF(hFile, szbuff, 1024, &dwBytesRead, NULL);
    }
	if (pfile)
	{
		fclose(pfile);
	}
	g_CloseH(hFile);
	return 0;
}
int scan(char *scanpath,char * extname)
{
	
	//	fopen(,"rb+")
	char scansym[512] = {0};
	sprintf(scansym,"%s\\*.%s",scanpath,extname);
	_finddata_t fileinfo;
	long lhand = _findfirst(scansym,&fileinfo);
	int data=0;
	if (!lhand)
	{
		return 1;
	}
	for (;data==0;)
	{
		if ((fileinfo.attrib&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY)
		{
			if (strcmp(fileinfo.name,"..") && strcmp(fileinfo.name,"."))
			{
				TCHAR newpath[512] = {0};
				sprintf(newpath,"%s\\%s",scanpath,fileinfo.name);
				scan(newpath,extname);
				
			}
		}
		if ((fileinfo.attrib&FILE_ATTRIBUTE_DIRECTORY)!=FILE_ATTRIBUTE_DIRECTORY)
		{
			TCHAR newpath[512] = {0};
			TCHAR mainpath[512] = {0};
			TCHAR subpath[512] = {0};
			sprintf(newpath,"%s\\%s",scanpath,fileinfo.name);
			sprintf(mainpath,"%s\\%s.%s",scanpath,fileinfo.name,extname);
			DatatoFile(newpath,mainpath);
			
		}
		data = _findnext(lhand,&fileinfo);
	}
	_findclose(lhand);
	return 0;
}
void CDlgMain::OnBnOpen() 
{
	// TODO: Add your control notification handler code here
	HANDLE hFile; 
	DWORD dwBytesRead =0;
	char szbuff[512] = {0};
	char szfilename[512] = {0};
	UpdateData(TRUE);
	g_CreateF = (IALibCreateFileA)GetProcAddress(::LoadLibraryA("IALib.DLL"),"M1");
	g_ReadF = (IALibReadFile)GetProcAddress(::LoadLibraryA("IALib.DLL"),"M6");
	g_CloseH = (IALibCloseHandle)GetProcAddress(::LoadLibraryA("IALib.DLL"),"M23");
	sprintf(szfilename,"%s.txt",m_csFilePath.GetBuffer(0));
	scan(m_csFilePath.GetBuffer(0),"txt");
	scan(m_csFilePath.GetBuffer(0),"bmp");
	scan(m_csFilePath.GetBuffer(0),"jpg");

// 	hFile = g_CreateF(m_csFilePath.GetBuffer(0),               // file to open
// 		GENERIC_READ,          // open for reading
// 		FILE_SHARE_READ,       // share for reading
// 		NULL,                  // default security
// 		OPEN_EXISTING,         // existing file only
// 		FILE_ATTRIBUTE_NORMAL, // normal file
// 		NULL);                 // no attr. template
// 	
//     if (hFile == INVALID_HANDLE_VALUE) 
//     { 
//         printf("Could not open file (error %d)\n", GetLastError());
//         return; 
//     }
// 	
//     if(!g_ReadF(hFile, szbuff, 511, &dwBytesRead, NULL))
//     {
//         printf("Could not read from file (error %d)\n", GetLastError());
//         return;
//     }
// 	g_CloseH(hFile);
// 	szbuff[511] = 0;
// 	m_csdata= szbuff;
	UpdateData(FALSE);

}
